# -*- coding:utf-8 -*-
# @FileName  :baidu_mapt1.py
# @Time      :2021/5/22 10:08
import json
import time
from multiprocessing import Pool
from save_mysql import mysql_checking
from sql_ac.sql_1 import select_all, insert_all, insert_all_adr
import requests
from fake_useragent import UserAgent
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
ua = UserAgent()
kv = {
    'user-agent': ua.random,
}


def main(w):
    r = requests.get(
        'https://map.baidu.com/?uid=' + str(w[4]) + '&ugc_type=3&ugc_ver=1&qt=detailConInfo&device_ratio=1&compat=1',
        headers=kv).text
    js = json.loads(r[r.find('{"content"'):])
    try:
        p = pp(str(js['content']['phone']).strip().replace("(", "").replace(")", ""))
    except KeyError:
        p = '1'
    if p != '1':
        if mysql_checking(p):
            insert_all_adr('baidu_map_spider', js['content']['name'].replace("'", "").replace("'", ""), js['content']['phone'],
                       p, w[3],
                       w[1], w[1], 11,
                       '0', 1, '', '', '', '', '', '', 2, w[3], w[5],
                       js['content']['addr'].replace("'", "").replace("'", ""), '', '', '', '', '', '', 0, '',
                       0, '', 0, 0,
                       2, 1, 0, str(time.strftime('%Y-%m-%d %H:%M:%S')), str(time.strftime('%Y-%m-%d %H:%M:%S')), w[5], 0,
                       0, '', 0,w[2])
            print('进一个')
    else:
        print('https://map.baidu.com/?uid=' + str(w[4]) + '&ugc_type=3&ugc_ver=1&qt=detailConInfo&device_ratio=1&compat=1')


def pp(e):
    if len(e.split(',')) > 1:
        for j in e.split(','):
            if len(j) == 11 and j[0] == '1' and j.find('-') == -1 and j.find('/') == -1:
                return j
            elif len(j) > 11 and j[-11] == '1' and j[-11:].find('-') == -1 and j[-11:].find('/') == -1:
                return j[:-11]
        return e.split(',')[0]
    if str(e) != 'None' and str(e) != 'null' and str(e) != '':
        if len(e) == 11 and e[0] == '1' and e.find('-') == -1 and e.find('/') == -1:
            return e
        elif len(e) > 11 and e[-11] == '1' and e[-11:].find('-') == -1 and e[-11:].find('/') == -1:
            return e[-11:]
        else:
            return '1'
    return '1'


if __name__ == "__main__":
    pool = Pool(20)
    for i in select_all('baidu_map')[2418289:]:
        # input(i)
        pool.apply_async(main, (i,))
    pool.close()
    pool.join()
    # for i in select_all('baidu_map')[4430:]:
    #     main(i)
